﻿/**  版本信息模板在安装目录下，可自行修改。
* DeliveryAddr.cs
*
* 功 能： N/A
* 类 名： DeliveryAddr
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2016/10/9 22:19:05   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:DeliveryAddr
	/// </summary>
	public partial class DeliveryAddr
	{
		public DeliveryAddr()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("DeliveryId", "DeliveryAddr"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int DeliveryId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from DeliveryAddr");
			strSql.Append(" where DeliveryId=@DeliveryId");
			SqlParameter[] parameters = {
					new SqlParameter("@DeliveryId", SqlDbType.Int,4)
			};
			parameters[0].Value = DeliveryId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.DeliveryAddr model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into DeliveryAddr(");
			strSql.Append("UserId,Name,Mobile,Phone,PostCode,Address,LocationId,IdentityNo,IdentityFrontPic,IdentityBackPic,IsMain,CreateTime,UpdateTime,IsActive)");
			strSql.Append(" values (");
			strSql.Append("@UserId,@Name,@Mobile,@Phone,@PostCode,@Address,@LocationId,@IdentityNo,@IdentityFrontPic,@IdentityBackPic,@IsMain,@CreateTime,@UpdateTime,@IsActive)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@PostCode", SqlDbType.VarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@LocationId", SqlDbType.Int,4),
					new SqlParameter("@IdentityNo", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityFrontPic", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityBackPic", SqlDbType.VarChar,50),
					new SqlParameter("@IsMain", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4)};
			parameters[0].Value = model.UserId;
			parameters[1].Value = model.Name;
			parameters[2].Value = model.Mobile;
			parameters[3].Value = model.Phone;
			parameters[4].Value = model.PostCode;
			parameters[5].Value = model.Address;
			parameters[6].Value = model.LocationId;
			parameters[7].Value = model.IdentityNo;
			parameters[8].Value = model.IdentityFrontPic;
			parameters[9].Value = model.IdentityBackPic;
			parameters[10].Value = model.IsMain;
			parameters[11].Value = model.CreateTime;
			parameters[12].Value = model.UpdateTime;
			parameters[13].Value = model.IsActive;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.DeliveryAddr model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update DeliveryAddr set ");
			strSql.Append("UserId=@UserId,");
			strSql.Append("Name=@Name,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("PostCode=@PostCode,");
			strSql.Append("Address=@Address,");
			strSql.Append("LocationId=@LocationId,");
			strSql.Append("IdentityNo=@IdentityNo,");
			strSql.Append("IdentityFrontPic=@IdentityFrontPic,");
			strSql.Append("IdentityBackPic=@IdentityBackPic,");
			strSql.Append("IsMain=@IsMain,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("UpdateTime=@UpdateTime,");
			strSql.Append("IsActive=@IsActive");
			strSql.Append(" where DeliveryId=@DeliveryId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
					new SqlParameter("@PostCode", SqlDbType.VarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@LocationId", SqlDbType.Int,4),
					new SqlParameter("@IdentityNo", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityFrontPic", SqlDbType.VarChar,50),
					new SqlParameter("@IdentityBackPic", SqlDbType.VarChar,50),
					new SqlParameter("@IsMain", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@DeliveryId", SqlDbType.Int,4)};
			parameters[0].Value = model.UserId;
			parameters[1].Value = model.Name;
			parameters[2].Value = model.Mobile;
			parameters[3].Value = model.Phone;
			parameters[4].Value = model.PostCode;
			parameters[5].Value = model.Address;
			parameters[6].Value = model.LocationId;
			parameters[7].Value = model.IdentityNo;
			parameters[8].Value = model.IdentityFrontPic;
			parameters[9].Value = model.IdentityBackPic;
			parameters[10].Value = model.IsMain;
			parameters[11].Value = model.CreateTime;
			parameters[12].Value = model.UpdateTime;
			parameters[13].Value = model.IsActive;
			parameters[14].Value = model.DeliveryId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int DeliveryId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DeliveryAddr ");
			strSql.Append(" where DeliveryId=@DeliveryId");
			SqlParameter[] parameters = {
					new SqlParameter("@DeliveryId", SqlDbType.Int,4)
			};
			parameters[0].Value = DeliveryId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string DeliveryIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from DeliveryAddr ");
			strSql.Append(" where DeliveryId in ("+DeliveryIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.DeliveryAddr GetModel(int DeliveryId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 DeliveryId,UserId,Name,Mobile,Phone,PostCode,Address,LocationId,IdentityNo,IdentityFrontPic,IdentityBackPic,IsMain,CreateTime,UpdateTime,IsActive from DeliveryAddr ");
			strSql.Append(" where DeliveryId=@DeliveryId");
			SqlParameter[] parameters = {
					new SqlParameter("@DeliveryId", SqlDbType.Int,4)
			};
			parameters[0].Value = DeliveryId;

			Maticsoft.Model.DeliveryAddr model=new Maticsoft.Model.DeliveryAddr();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.DeliveryAddr DataRowToModel(DataRow row)
		{
			Maticsoft.Model.DeliveryAddr model=new Maticsoft.Model.DeliveryAddr();
			if (row != null)
			{
				if(row["DeliveryId"]!=null && row["DeliveryId"].ToString()!="")
				{
					model.DeliveryId=int.Parse(row["DeliveryId"].ToString());
				}
				if(row["UserId"]!=null && row["UserId"].ToString()!="")
				{
					model.UserId=int.Parse(row["UserId"].ToString());
				}
				if(row["Name"]!=null)
				{
					model.Name=row["Name"].ToString();
				}
				if(row["Mobile"]!=null)
				{
					model.Mobile=row["Mobile"].ToString();
				}
				if(row["Phone"]!=null)
				{
					model.Phone=row["Phone"].ToString();
				}
				if(row["PostCode"]!=null)
				{
					model.PostCode=row["PostCode"].ToString();
				}
				if(row["Address"]!=null)
				{
					model.Address=row["Address"].ToString();
				}
				if(row["LocationId"]!=null && row["LocationId"].ToString()!="")
				{
					model.LocationId=int.Parse(row["LocationId"].ToString());
				}
				if(row["IdentityNo"]!=null)
				{
					model.IdentityNo=row["IdentityNo"].ToString();
				}
				if(row["IdentityFrontPic"]!=null)
				{
					model.IdentityFrontPic=row["IdentityFrontPic"].ToString();
				}
				if(row["IdentityBackPic"]!=null)
				{
					model.IdentityBackPic=row["IdentityBackPic"].ToString();
				}
				if(row["IsMain"]!=null && row["IsMain"].ToString()!="")
				{
					model.IsMain=int.Parse(row["IsMain"].ToString());
				}
				if(row["CreateTime"]!=null && row["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(row["CreateTime"].ToString());
				}
				if(row["UpdateTime"]!=null && row["UpdateTime"].ToString()!="")
				{
					model.UpdateTime=DateTime.Parse(row["UpdateTime"].ToString());
				}
				if(row["IsActive"]!=null && row["IsActive"].ToString()!="")
				{
					model.IsActive=int.Parse(row["IsActive"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select DeliveryId,UserId,Name,Mobile,Phone,PostCode,Address,LocationId,IdentityNo,IdentityFrontPic,IdentityBackPic,IsMain,CreateTime,UpdateTime,IsActive ");
			strSql.Append(" FROM DeliveryAddr ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" DeliveryId,UserId,Name,Mobile,Phone,PostCode,Address,LocationId,IdentityNo,IdentityFrontPic,IdentityBackPic,IsMain,CreateTime,UpdateTime,IsActive ");
			strSql.Append(" FROM DeliveryAddr ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM DeliveryAddr ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.DeliveryId desc");
			}
			strSql.Append(")AS Row, T.*  from DeliveryAddr T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "DeliveryAddr";
			parameters[1].Value = "DeliveryId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

